﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using JWTAuthorize;

namespace ServiceA
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //从配置文件中读取issuer，密钥等信息
            var audienceConfig = Configuration.GetSection("Audience");
            var Issuer = audienceConfig["Issuer"];
            var Secret = audienceConfig["Secret"];
            services.AddAPIPolicyJwtBearer(Issuer, Issuer, Secret, "JWTBearer", "JWTPolicy", "/authapi/login");//验证不通过时跳转到登录页面

            //权限集合，正式环境下从数据库读取
            var permission = new List<RolePermission> {
                              new RolePermission {  Url="/api/valuesa", Name="admin"},
                              new RolePermission {  Url="/", Name="admin"}
                          };
            services.AddSingleton(permission);//依赖注入权限列表
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));//在控制台界面打印日志
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
}
